package it.valeandre.omino.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Pair;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.simpleframework.xml.ElementListUnion;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import it.valeandre.omino.R;
import it.valeandre.omino.model.Match;
import it.valeandre.omino.utils.TypedPair;
import it.valeandre.omino.utils.Utils;

/**
 * Created by Getta on 26/12/13.
 */
public class TimelineView extends LinearLayout {

    private final float DEFAULT_TIME_RATIO = 15;
    private final int DEFAULT_VERTICAL_SEPARATOR_RESID = 0;
    private final int DEFAULT_VERTICAL_SEPARATOR_WIDTH = 40;
    private final int DEFAULT_VERTICAL_SEPARATOR_MARGIN = 15;
    private final int DEFAULT_SEPARATOR_COLOR = 0xFFF4FF92;

    private final int LEFT_COLUMN = 0;
    private final int RIGHT_COLUMN = 1;
    protected final int EVENT_TYPE_YELLOW_CARD = 0;
    protected final int EVENT_TYPE_RED_CARD = 1;
    protected final int EVENT_TYPE_GOAL = 2;

    private float mTimeRatio = DEFAULT_TIME_RATIO;
    private int mVerticalSeparatorResId;
    private int mVerticalSeparatorWidth;
    private FrameLayout mLeftContainer, mRightContainer;

    public TimelineView(Context context) {
        super(context);
        init(context, null);
    }

    public TimelineView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public TimelineView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray a = context.getTheme().obtainStyledAttributes(
                    attrs,
                    R.styleable.TimelineView,
                    0, 0);

            try {
                mTimeRatio = a.getFloat(R.styleable.TimelineView_timeRatio, DEFAULT_TIME_RATIO);
                mVerticalSeparatorResId = a.getResourceId(R.styleable.TimelineView_verticalSeparatorRes, DEFAULT_VERTICAL_SEPARATOR_RESID);
                mVerticalSeparatorWidth = a.getDimensionPixelSize(R.styleable.TimelineView_verticalSeparatorWidth, DEFAULT_VERTICAL_SEPARATOR_WIDTH);
            } finally {
                a.recycle();
            }
        }
        setOrientation(LinearLayout.HORIZONTAL);
        createColumns();
    }

    private void createColumns() {

        LinearLayout.LayoutParams columnParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1f);
        mLeftContainer = new FrameLayout(getContext());
        mRightContainer = new FrameLayout(getContext());
        mLeftContainer.setLayoutParams(columnParams);
        mRightContainer.setLayoutParams(columnParams);

        addView(mLeftContainer);

        //ADD SEPARATOR HERE
        addView(fillScale());

        addView(mRightContainer);

    }

    private FrameLayout fillScale() {
        LinearLayout.LayoutParams separatorParams = new LinearLayout.LayoutParams((int) Utils.dipToPixels(getContext(), DEFAULT_VERTICAL_SEPARATOR_WIDTH), ViewGroup.LayoutParams.MATCH_PARENT);
        FrameLayout verticalSeparator = new FrameLayout(getContext());
        separatorParams.leftMargin = (int) Utils.dipToPixels(getContext(), DEFAULT_VERTICAL_SEPARATOR_MARGIN);
        separatorParams.rightMargin = (int) Utils.dipToPixels(getContext(), DEFAULT_VERTICAL_SEPARATOR_MARGIN);
        verticalSeparator.setLayoutParams(separatorParams);
        verticalSeparator.setBackgroundColor(DEFAULT_SEPARATOR_COLOR);

        for(int i=1; i<10; i++ )  {

            int scaleValue = (int) Utils.dipToPixels(getContext(), i * 10 * DEFAULT_TIME_RATIO);
            FrameLayout.LayoutParams textValueParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            textValueParams.topMargin = scaleValue;
            TextView valueText = new TextView(getContext()) ;
            valueText.setLayoutParams(textValueParams);
            valueText.setGravity(Gravity.CENTER_HORIZONTAL);
            valueText.setText((i*10) + "°");
            verticalSeparator.addView(valueText);

        }

        return verticalSeparator;

    }

    public void fill(Match match) {
        List<TypedPair<String, String>> goalDetailsPairList = match.getHomeGoalDetailsPairList().getEventDetailPairList();
        List<TypedPair<String, String>> yellowCardsDetailsPairList = match.getHomeYellowCardsDetailsPairList().getEventDetailPairList();
        List<TypedPair<String, String>> redCardsDetailsPairList = match.getHomeRedCardsDetailsPairList().getEventDetailPairList();

        fillFrameContainer(mLeftContainer, goalDetailsPairList, yellowCardsDetailsPairList, redCardsDetailsPairList, LEFT_COLUMN);

        goalDetailsPairList = match.getAwayGoalDetailsPairList().getEventDetailPairList();
        yellowCardsDetailsPairList = match.getAwayYellowCardsDetailsPairList().getEventDetailPairList();
        redCardsDetailsPairList = match.getAwayRedCardsDetailsPairList().getEventDetailPairList();

        fillFrameContainer(mRightContainer, goalDetailsPairList, yellowCardsDetailsPairList, redCardsDetailsPairList, RIGHT_COLUMN);

    }

    private void fillFrameContainer(FrameLayout container, List<TypedPair<String, String>> goalDetailsPairList, List<TypedPair<String, String>> yellowCardsDetailsPairList, List<TypedPair<String, String>> redCardsDetailsPairList, int column) {

        for (Pair<String, String> goal : goalDetailsPairList) {
            LinearLayout eventView = createEvent(goal.first, EVENT_TYPE_GOAL, goal.second, column);
            container.addView(eventView);
        }

        for (Pair<String, String> yellowCard : yellowCardsDetailsPairList) {
            LinearLayout eventView = createEvent(yellowCard.first, EVENT_TYPE_YELLOW_CARD, yellowCard.second, column);
            container.addView(eventView);
        }

        for (Pair<String, String> redCard : redCardsDetailsPairList) {
            LinearLayout eventView = createEvent(redCard.first, EVENT_TYPE_RED_CARD, redCard.second, column);
            container.addView(eventView);
        }

    }

    private LinearLayout createEvent(String minute, int type, String player, int column) {
        FrameLayout.LayoutParams eventContainerParams = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        LinearLayout eventContainer = new LinearLayout(getContext());
        eventContainerParams.topMargin = (int) Utils.dipToPixels(getContext(), Integer.parseInt(minute.replace("'", "")) * mTimeRatio);

        int gravity = column == LEFT_COLUMN ? Gravity.RIGHT : Gravity.LEFT;

        eventContainer.setGravity(gravity);

        eventContainer.setOrientation(LinearLayout.VERTICAL);
        eventContainer.setLayoutParams(eventContainerParams);

        LinearLayout.LayoutParams textContainerParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        LinearLayout eventTextContainer = new LinearLayout(getContext());
        eventTextContainer.setLayoutParams(textContainerParams);

        LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        TextView minuteTextView = new TextView(getContext());
        minuteTextView.setLayoutParams(textParams);
        minuteTextView.setText(minute);

        LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams((int)Utils.dipToPixels(getContext(), 16), (int)Utils.dipToPixels(getContext(), 16));
        ImageView image = new ImageView(getContext());
        image.setLayoutParams(imageParams);
        if(type == EVENT_TYPE_GOAL) {
            image.setImageResource(R.drawable.palla_calcio);
        } else if(type == EVENT_TYPE_RED_CARD) {
            image.setImageResource(R.drawable.red_card);
        } else {
            image.setImageResource(R.drawable.yellow_card);
        }

        TextView playerTextView = new TextView(getContext());
        playerTextView.setLayoutParams(textParams);
        playerTextView.setText(player.trim());

        if (column == LEFT_COLUMN) {
            eventTextContainer.addView(minuteTextView);
            eventTextContainer.addView(image);
        } else {
            eventTextContainer.addView(image);
            eventTextContainer.addView(minuteTextView);
        }

        eventContainer.addView(eventTextContainer);
        eventContainer.addView(playerTextView);

        return eventContainer;

    }


}
